﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web.Mvc;
using QDT.Common;
using QDT.Common.Data;
using QDT.Common.Finance;
using QDT.Core;
using QDT.Core.Assignments;
using QDT.Core.Deals;
using QDT.Core.Domain;
using QDT.Core.Members;
using QDT.Core.Projects;

namespace QDT.Web
{

    public class FunctionHelper {
        public IDbProvider DbProvider { get { return IoC.Resolve<IUnitOfWork>() as IDbProvider; } }

        /// <summary>
        /// 获取地区
        /// </summary>
        /// <param name="id">地区ID</param>
        /// <returns></returns>
        public string GetRegionById(int id)
        {
            var region = DbProvider.GetByID<RegionConfig>(id);
            return region == null ? "不存在" : region.Name;
        }

        /// <summary>
        /// 获取银行卡
        /// </summary>
        /// <param name="id">地区ID</param>
        /// <returns></returns>
        public string GetBankById(int id)
        {
            var banl = DbProvider.GetByID<Bank>(id);
            return banl == null ? "不存在" : banl.Name;
        }

        /// <summary>
        /// 获得用户绑定的银行卡数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetUserHasBank(int id)
        {
            return DbProvider.D<UserBank>().Count(x => x.UserId == id);
        }

        public string GetUserSafeLevel(User user)
        {
            var hasBank = GetUserHasBank(user.ID);
            var i = 1;
            if (user.IDCardPassed) { i++; }
            if (user.MobilePassed) { i++; }
            if (user.EmailPassed) { i++; }
            if (user.SafePasswordPassed) { i++; }
            if (hasBank > 0) { i++; }

            return (i * 100 / 6).ToString("0");
        }

        /// <summary>
        /// 获取项目名
        /// </summary>
        /// <param name="id">项目ID</param>
        /// <returns></returns>
        public string GetProjectName(int id)
        {
            var project = DbProvider.GetByID<Project>(id);
            return project == null ? "不存在" : project.Name;
        }

        /// <summary>
        /// 获取项目
        /// </summary>
        /// <param name="id">项目ID</param>
        /// <returns></returns>
        public Project GetProject(int id)
        {
            var project = DbProvider.GetByID<Project>(id);
            return project;
        }

        /// <summary>
        /// 获取项目限期
        /// </summary>
        /// <param name="id">项目ID</param>
        /// <returns></returns>
        public string GetProjectRepayTime(int id)
        {
            var project = DbProvider.GetByID<Project>(id);
            return project == null ? "不存在" : project.RepayTime + "个月";
        }

        /// <summary>
        /// 获取项目进度
        /// </summary>
        /// <param name="id">项目ID</param>
        /// <returns></returns>
        public string GetProjectProgress(int id)
        {
            var project = DbProvider.GetByID<Project>(id);
            return project == null ? "0%" : Math.Round((decimal)(project.CurrentStage/project.TotalStage)) + "%";
        }

        public ICollection<ProjectFeild> GetProjectFeilds(Project project)
        {
            return DbProvider.D<ProjectFeild>().Where(x => x.TypeID == project.TypeID).ToList();
        } 

        /// <summary>
        /// 获取用户名
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns></returns>
        public string GetUserName(int id)
        {
            var user = DbProvider.GetByID<User>(id);
            return user == null ? "不存在" :  user.UserName;
        }

        public string GetLoadUserName(int assignmentId)
        {
            var assign = DbProvider.GetByID<Assignment>(assignmentId);
            var credit = DbProvider.GetByID<Credit>(assign.CreditId);
            return credit == null ? "不存在" : credit.UserName;
        }

        public User GetUser(int id)
        {
            return DbProvider.GetByID<User>(id);
        }

        public UserAccount GetUserAccount(int id)
        {
            return DbProvider.GetByID<UserAccount>(id);
        }

        public Credit GetCredit(int id)
        {
            return DbProvider.GetByID<Credit>(id);
        }

        public ProjectAgency GetAgency(int? id)
        {
            if (id == null || id == 0)
            {
                return null;
            }
            return DbProvider.GetByID<ProjectAgency>(id);
        }

        public Assignment GetAssig(int? id)
        {
            if (id == null || id == 0)
            {
                return null;
            }
            return DbProvider.GetByID<Assignment>(id);
        }

        public UserPrize GetUserPrize(int userid)
        {
            return DbProvider.D<UserPrize>().FirstOrDefault(x => x.UserId == userid);
        }

        public CreditExtension GetCreditExtension(int cid)
        {
            return DbProvider.D<CreditExtension>().FirstOrDefault(x => x.CreditId==cid);
        }

        public string SubString(string str, int length=16)
        {
            if (str.Length < length) return str;
            return str.Substring(0, length-2) + "...";
        }

        public decimal GetInterest(decimal money, int day, double annualRate)
        {
            return InterestAlgorithm.DailyInterest(day, money, annualRate/100);
        }

    }

    public static class FunctionHelperExntensions
    {
        private static readonly FunctionHelper functionHelper=new FunctionHelper();

        public static FunctionHelper F(this HtmlHelper page)
        {
            return functionHelper;
        }
    }
}